package com.example.user.demo1;

import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.ViewUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListPopupWindow;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;


public class MainActivity extends AppCompatActivity {

    @ViewInject(R.id.text1)
    TextView text1;

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        ViewInjectUtils.inject(this);
        initView();
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    private void initView(){
//        text1 = findViewById(R.id.text1);
        final List<String> list = new ArrayList<>();
        Adapter adapter = new Adapter(list);
        list.add("2222");
        list.add("3333");
        list.add("4444");
//        final ListPopupWindow popupWindow = new ListPopupWindow(this);
//        popupWindow.setAnchorView(text1);
//        popupWindow.setWidth(ListPopupWindow.WRAP_CONTENT);
////        popupWindow.setDropDownGravity(Gravity.END);
//        popupWindow.setHeight(ListPopupWindow.WRAP_CONTENT);
//        popupWindow.setModal(true);
//        popupWindow.setAdapter(adapter);

        final CustomedPopu popu = new CustomedPopu(this, list, new CallBack() {
            @Override
            public void click(int position) {

                text1.setText(list.get(position));
            }
        });


//        popupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//
//            }
//        });
        text1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popu.showAsDropDown(text1, Gravity.LEFT,0,0);
            }
        });

        RecyclerView recyclerView;
        ListView listView;
    }

    class Adapter extends BaseAdapter{
        List<String> list;
        Adapter(List<String> list){
            this.list = list;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            Log.e("1111",position + "");

            ViewHolder holder = null;
            if (convertView == null){
                holder = new ViewHolder();
                convertView= getLayoutInflater().inflate(R.layout.item_layout,null);
                holder.text = convertView.findViewById(R.id.text1);
                convertView.setTag(holder);

            }else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.text.setText(list.get(position));

            return convertView;
        }

        class ViewHolder{
            TextView text;
        }
    }



}
